<?php
/**
 * Created by PhpStorm.
 * User: 曹旺
 * Date: 2018-05-21
 * Time: 13:04
 */

namespace app\index\controller;

use think\Controller;

class Exigency extends Controller
{
    /**
     * @return mixed
     * 紧急事件处理申请页面
     * @author  曹旺
     * @data  13:09
     */
    public function index()
    {
        $ExigencyModel = new \app\index\model\Exigency();
        //审批流程
        $ExigencyFlow = $ExigencyModel->flow_exigency_apply();
        $this->assign('ExigencyFlow', $ExigencyFlow);
        return $this->fetch();
    }

    /**
     * @return false|int
     * 紧急事件添加数据处理
     * @author  曹旺
     * @data  15:18
     */
    public function approve_sub()
    {
        session_start();
        $data = json_decode(input('post.data'), true);
        $id = $_SESSION['id'];
        $ExigencyModel = new \app\index\model\Exigency();
        $res = $ExigencyModel->exigency_apply_sub($data, $id);
        return $res;
    }

    /**
     * @return mixed
     * 紧急事件申请列表页面
     * @author  曹旺
     * @data  15:18
     */
    public function exigency_approve_list()
    {
        return $this->fetch();
    }

    /**
     * @return array
     * 紧急事件申请列表table数据
     * @author  曹旺
     * @data  15:17
     */
    public function exigency_approve_list_table()
    {
        session_start();
        if (isset($_SESSION['id'])) {
            //判断用户id
            $page = input('get.page');
            if ($page == '') {
                $page = 1;
            }
            $num = input('get.limit');
            if ($num == '') {
                $num = 10;
            }
            $user_id = $_SESSION['id'];
            $where = 'state = 1 and exigency_user_id=' . $user_id;
            //取相关数据
            $ExigencySchedule = new \app\index\model\Exigency();
            $res = $ExigencySchedule->exigency_approve_list_table($where, $page, $num);
            //dump($res);die;
            return $res;
        } else {

        }
    }

    /**
     * @return mixed
     * 申请详情
     * @author  曹旺
     * @data  09:39
     */
    public function exigency_approve_info()
    {
        //项目进度id
        $ExigencyId = input('get.id');
        $ExigencyModel = new \app\index\model\Exigency();
        //取项目进度的相关信息
        $ExigencyExigencyInfo = $ExigencyModel::get($ExigencyId);
        $stateApply = json_decode($ExigencyExigencyInfo->apply_state, true);
        //审批流程
        $flow = \app\index\model\Flow::get($ExigencyExigencyInfo->exigency_flow_id);
        //审批流程的次数
        $num = 0;
        if ($flow->first != '') {
            $num++;
        }
        if ($flow->second != '') {
            $num++;
        }
        if ($flow->third != '') {
            $num++;
        }
        if ($flow->fourth != '') {
            $num++;
        }
        if ($flow->fifth != '') {
            $num++;
        }
        if ($flow->sixth != '') {
            $num++;
        }
        if ($flow->seventh != '') {
            $num++;
        }
        if ($flow->eighth != '') {
            $num++;
        }
        if ($flow->ninth != '') {
            $num++;
        }
        if ($flow->tenth != '') {
            $num++;
        }
        //echo $num;
        $count = count($stateApply) - 1;
        if (count($stateApply) == 1 && $stateApply['0']['state'] == 2) {
            $state = 1;
        } else {
            $state = 0;
        }
        if ($stateApply[$count]['state'] == 2) {
            $ExigencyExigencyInfo['apply_state'] = $stateApply[$count]['audit_opinion'];
            $audit_opinion = $stateApply[$count]['audit_opinion'];
        } else {
            $ExigencyExigencyInfo['apply_state'] = '';
            if ($count == $num - 1 && $stateApply[$count]['state'] == 1) {
                $audit_opinion = $stateApply[$count]['audit_opinion'];
            } else {
                $audit_opinion = '正在审批中...';
            }
        }
        //$Exigency = new \app\index\model\Project();
        //取项目相关信息（用到项目名和ID）
        if ($state == 1) {
            $ExigencyFlow = $ExigencyModel->flow_exigency_apply();
        } else {
            $ExigencyFlow = $ExigencyModel->flow_exigency_apply($ExigencyExigencyInfo['exigency_flow_id']);
        }
        $this->assign('ExigencyFlow', $ExigencyFlow);
        $this->assign('ExigencyInfo', $ExigencyExigencyInfo);
        $this->assign('state', $state);
        $this->assign('audit_opinion', $audit_opinion);
        return $this->fetch();
    }

    /**
     * @return mixed
     * 审批列表
     * @author  曹旺
     * @data  09:40
     */
    public function exigency_apply_list()
    {
        session_start();
        $where = 'state = 1';
        if ($this->request->isPost()) {
            $id = input('post.id');
        } else {
            $id = '';
        }
        //判断用户id
        //dump($id);
        //$where = 'state = 1 and project_user_id=' . $user_id.' and id='.$id;
        //取相关数据
        $ExigencyModel = new \app\index\model\Exigency();
        $Exigency = $ExigencyModel->exigency_apply_list($where, $id);
        //dump($Exigency);
        $this->assign('exigency', $Exigency);
        return $this->fetch();
    }

    /**
     * @return mixed
     * 审批详情
     * @author  曹旺
     * @data  09:39
     */
    public function exigency_apply_info()
    {
        //项目进度id
        $ExigencyId = input('get.id');
        $ExigencyModel = new \app\index\model\Exigency();
        //取项目进度的相关信息
        $ExigencyIdInfo = $ExigencyModel::get($ExigencyId);
        //$Project = new \app\index\model\Project();
        //取项目相关信息（用到项目名和ID）
        //$ProjectName = $Project::get($ExigencyIdInfo['project_id']);
        //dump($ProjectName);
        $ExigencyFlow = $ExigencyModel->flow_exigency_apply($ExigencyIdInfo['exigency_flow_id']);
        //dump($ProjectFlow);die;
        //$ProjectName=$Project->field('id ,project_name')->where(['project_state'=>1])->select()->toArray();
        //$this->assign('ProjectName', $ProjectName);
        $this->assign('ExigencyFlow', $ExigencyFlow);
        $this->assign('ExigencyInfo', $ExigencyIdInfo);
        return $this->fetch();
    }

    /**
     * @return int
     * 删除申请（软删除）
     * @author  曹旺
     * @data  09:38
     */
    public function exigency_del()
    {
        $id = input('post.id');
        $res = \app\index\model\Exigency::destroy($id);
        return $res;

    }

    /**
     * @return false|int
     * 紧急事件审批
     * @author  曹旺
     * @data  09:55
     */
    public function exigency_apply_agree()
    {
        session_start();
        //申请数据的id
        $id = input('post.id');
        //审批意见
        $audit_opinion = input('post.audit_opinion');
        if ($audit_opinion == '') {
            $audit_opinion = '无';
        }
        //审批用户的id
        $user_id = $_SESSION['id'];
        //用户拥有的角色
        $user_role = model('UserRole')->field('role_id')->where(['user_id' => $user_id, 'state' => 1])->select()->toArray();
        //申请数据
        /*$apply_state = model('ProjectSchedule')->field('apply_state,project_flow_id')->where(['id' => $id])
            ->find();*/
        $apply_state = \app\index\model\Exigency::get($id);
        //取对应审批流程
        //$flow=model('Flow')->get($apply_state['project_flow_id'])->toArray();
        $flow = model('Flow')->field('first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth')
            ->where(['id' => $apply_state['exigency_flow_id']])->find()->toArray();
        //取对应审批流程的审批次数
        $flow_num = count(array_filter($flow));
        //取对应审批流程审批的审批级别（取键名）
        $flow_key = array_keys($flow);
        $flow_key_num = count($flow_key);
        $apply_state_array = json_decode($apply_state['apply_state'], true);
        //取出json串中数组的个数（审批的个数）
        $count = count($apply_state_array);
        //当前审批的下标
        $index = $count - 1;
        //return $index;
        foreach ($user_role as $key => $value) {
            if ($apply_state_array[$index]['role'] == $flow[$apply_state_array[$index]['role_key']] && $apply_state_array[$index]['role'] == $value['role_id']) {
                //判断流程的级数不能大于审批的次数
                if ($flow_num > $count) {
                    if ($apply_state_array[$index]['state'] == 1) {
                        return 2;
                    }
                    if ($apply_state_array[$index]['state'] == 2) {
                        return 3;
                    }
                    $apply_state_array[$index]['user_id'] = $user_id;
                    $apply_state_array[$index]['audit_opinion'] = $audit_opinion;
                    $apply_state_array[$index]['audit_time'] = time();
                    $apply_state_array[$index]['audit_name'] = $_SESSION['ch_name'];
                    $apply_state_array[$index]['state'] = 1;
                    for ($i = 0; $i < $flow_key_num; $i++) {
                        if ($flow_key[$i] == $apply_state_array[$index]['role_key']) {
                            $apply_state_array[$count]['role'] = $flow[$flow_key[$i + 1]];
                            $apply_state_array[$count]['user_id'] = '';
                            $apply_state_array[$count]['role_key'] = $flow_key[$i + 1];
                            $apply_state_array[$count]['audit_opinion'] = '';
                            $apply_state_array[$count]['audit_time'] = '';
                            $apply_state_array[$count]['audit_name'] = '';
                            $apply_state_array[$count]['state'] = 0;
                        }
                    }
                    $apply_state_json = json_encode($apply_state_array);
                    $apply_state_new['apply_state'] = $apply_state_json;
                    $result = $apply_state->save($apply_state_new);
                    //$result = Db::table('oa_expense_reimbursement')->where(['id' => $id])->update($apply_state_new);
                    return $result;
                } else {
                    if ($apply_state_array[$index]['state'] == 1) {
                        return 2;
                    }
                    if ($apply_state_array[$index]['state'] == 2) {
                        return 3;
                    }
                    $apply_state_array[$index]['user_id'] = $user_id;
                    $apply_state_array[$index]['audit_opinion'] = $audit_opinion;
                    $apply_state_array[$index]['audit_time'] = time();
                    $apply_state_array[$index]['audit_name'] = $_SESSION['ch_name'];
                    $apply_state_array[$index]['state'] = 1;
                    $apply_state_json = json_encode($apply_state_array);
                    $apply_state_new['apply_state'] = $apply_state_json;
                    $apply_state_new['apply_success'] = 1;
                    $result = $apply_state->save($apply_state_new);
                    //$result = Db::table('oa_expense_reimbursement')->where(['id' => $id])->update($apply_state_new);
                    return $result;
                }
            } else {
                if ($key == (count($user_role) - 1)) {
                    return 2;
                }
            }
        }
    }

    /**
     * @return false|int
     * 紧急事件驳回
     * @author  曹旺
     * @data  09:56
     */
    public function exigency_apply_disagree()
    {
        session_start();
        //申请数据的id
        $id = input('post.id');
        //审批意见
        $audit_opinion = input('post.audit_opinion');
        if ($audit_opinion == '') {
            $audit_opinion = '无';
        }
        //审批用户的id
        $user_id = $_SESSION['id'];
        //用户拥有的角色
        $user_role = model('UserRole')->field('role_id')->where(['user_id' => $user_id, 'state' => 1])->select()->toArray();
        //申请数据
        /*$apply_state = model('ProjectSchedule')->field('apply_state,project_flow_id')->where(['id' => $id])
            ->find();*/
        $apply_state = \app\index\model\Exigency::get($id);
        //取对应审批流程
        //$flow=model('Flow')->get($apply_state['project_flow_id'])->toArray();
        $flow = model('Flow')->field('first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth')
            ->where(['id' => $apply_state['exigency_flow_id']])->find()->toArray();
        //取对应审批流程的审批次数
        $flow_num = count(array_filter($flow));
        //取对应审批流程审批的审批级别（取键名）
        $flow_key = array_keys($flow);
        $flow_key_num = count($flow_key);
        $apply_state_array = json_decode($apply_state['apply_state'], true);
        //取出json串中数组的个数（审批的个数）
        $count = count($apply_state_array);
        //当前审批的下标
        $index = $count - 1;
        //return $index;
        foreach ($user_role as $key => $value) {
            if ($apply_state_array[$index]['role'] == $flow[$apply_state_array[$index]['role_key']] && $apply_state_array[$index]['role'] == $value['role_id']) {
                //判断流程的级数不能大于审批的次数
                if ($apply_state_array[$index]['state'] == 1) {
                    return 3;
                }
                if ($apply_state_array[$index]['state'] == 2) {
                    return 2;
                }
                $apply_state_array[$index]['user_id'] = $user_id;
                $apply_state_array[$index]['audit_opinion'] = $audit_opinion;
                $apply_state_array[$index]['audit_time'] = time();
                $apply_state_array[$index]['audit_name'] = $_SESSION['ch_name'];
                $apply_state_array[$index]['state'] = 2;
                $apply_state_json = json_encode($apply_state_array);
                $apply_state_new['apply_state'] = $apply_state_json;
                $apply_state_new['apply_success'] = 2;
                $result = $apply_state->save($apply_state_new);
                //$result = Db::table('oa_expense_reimbursement')->where(['id' => $id])->update($apply_state_new);
                return $result;

            } else {
                if ($key == (count($user_role) - 1)) {
                    return 3;
                }
            }
        }
    }
}